package com.minelx.statementplus.ui.form.ui;

import com.minelx.landisk.filesystem.Paths;
import com.minelx.landisk.filesystem.core.File;
import com.minelx.landisk.filesystem.core.init.OpenPolicy;
import com.minelx.statementplus.ui.form.service.tablemapping.TableMappingFactory;
import com.minelx.statementplus.ui.form.ui.memberbased.ChildSqlBuildingDialog;
import com.minelx.statementsplus.CombinedSql;
import com.minelx.statementsplus.MajorSql;
import com.minelx.statementsplus.Majors;
import com.minelx.statementsplus.Proxy;
import com.minelx.woods.task.core.SqlTaskService;
import com.minelx.woods.task.core.SqlTaskValues;

import javax.swing.*;
import java.awt.*;
import java.util.Collections;
import java.util.List;

public class DownloadService {
	private final SqlTaskService sqlTaskService;

	private final DefaultListModel<File> addingFile;

	public DownloadService(SqlTaskService sqlTaskService, DefaultListModel<File> addingFile) {
		this.sqlTaskService = sqlTaskService;
		this.addingFile = addingFile;
	}

	public void buildAndDownloadWithWholeProxy(Window owner) {
		TableMappingFactory tableMappingFactory = TableMappingFactory.configuredMappings();
		new ChildSqlBuildingDialog(owner,
				null,
				tableMappingFactory.createMapping(List.of("memberId", "salesmanId", "proxyId")),
				childSqlList -> {
					String fileName = JOptionPane.showInputDialog(owner, "下载的文件名称叫什么?");
					if (fileName == null) {
						return;
					}
					for (Proxy province : Proxy.provinces().children()) {
						try {
							System.out.println("start province.name() = " + province.name());
							MajorSql major = Majors.proxies(new int[]{province.code()});
							final CombinedSql combined = new CombinedSql(major, childSqlList);
							File destinationFile = downloadedFile(fileName + "_" + province.name());
							SqlTaskValues sqlTask = new SqlTaskValues(combined, destinationFile, Collections.emptyMap());
							download(destinationFile, sqlTask);
						} catch (Exception e) {
							System.err.println("error while downloading: " + province.name());
						}
					}
					JOptionPane.showMessageDialog(owner, "下载完成");
				}).setVisible(true);
	}

	public void buildAndDownload(Window owner, MajorSql major) {
		TableMappingFactory tableMappingFactory = TableMappingFactory.configuredMappings();
		new ChildSqlBuildingDialog(owner,
				major,
				tableMappingFactory.createMapping(major.head().mappingKeys()),
				childSqlList -> {
					String fileName = JOptionPane.showInputDialog(owner, "下载的文件名称叫什么?");
					if (fileName == null) {
						return;
					}
					File destinationFile = downloadedFile(fileName);
					SqlTaskValues sqlTask = new SqlTaskValues(new CombinedSql(major, childSqlList), destinationFile, Collections.emptyMap());
					download(destinationFile, sqlTask);
					JOptionPane.showMessageDialog(owner, "下载完成");
				}).setVisible(true);
	}

	private File downloadedFile(String name) {
		return Paths.projectRoot()
				.child().dir("downloaded_files", OpenPolicy.useExistingOrCreateANewOne())
				.child().file(name, OpenPolicy.createANewOne());
	}

	private void download(File destinationFile, SqlTaskValues sqlTask) {
		// build task
		try {
			// FIXME 2021/9/26  wait for me!!!      remove filterer
			sqlTaskService.submit(sqlTask).waitForCompletion();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		// add file
		addingFile.addElement(destinationFile);
	}
}
